/* 
 *  Copyright 2012 CodeMagi, Inc.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.codemagi.servlets.validation;

import com.codemagi.servlets.AppException;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;

/**
 * Validates that an input String is a valid Java TimeZone identifier.
 */
public class TimeZoneValidator implements IValidator {

    Logger log = Logger.getLogger(this.getClass());

    private static final Set<String> options = new HashSet<String>();
    static {
	for (String zoneId : TimeZone.getAvailableIDs()) {
	    options.add(zoneId);
	}
    }

    public Object validate(Object obj, HttpServletRequest request) throws AppException {
	if (obj instanceof String) {

	    String input = (String)obj;
	    log.debug("INPUT: " + input);

	    if (!options.contains(input)) throw new AppException("Invalid time zone specified.");

	    return input;
	}

	return obj;
    }

}
